﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Shrestha.Trigraph
{
    /// <summary>
    /// Interaction logic for TriGraphSearch.xaml
    /// </summary>
    public partial class TriGraphSearch : Window
    {
        private WorkingSet workingSet;
        private string searchString;
        
        public WorkingSet Result { get; private set; }


        public TriGraphSearch(WorkingSet ws)
        {
            workingSet = ws;
            searchString = null; 
            InitializeComponent();
            this.comboBoxRows.ItemsSource = TriGraphResources.rowNames;
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            if (searchString != null) searchString += " and ";
            searchString += comboBoxRows.SelectedValue;
            if (comboBoxOperator.SelectedIndex == 0) searchString += " between " + minBox.Text + " and " + maxBox.Text;
            else searchString += " = " + minBox.Text;
            textSearchString.Text = searchString;
        }

        private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Result = Filter.GetFiltered(textSearchString.Text==null?textSearchString.Text:searchString, workingSet.IsClustered);
                this.DialogResult = true;
            }
            catch
            {
                MessageBox.Show("Error: Please enter valid range for the filters");
                this.DialogResult = false;
            }
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void comboBoxOperator_DropDownClosed(object sender, EventArgs e)
        {
           if((sender as ComboBox).SelectionBoxItem.ToString().Equals("Between")) maxBox.IsEnabled = true; else maxBox.IsEnabled = false;
        }
    }
}
